package com.ljx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.common.ResponseResult;
import com.ljx.domain.dto.RoleStatusDto;
import com.ljx.domain.entity.Role;
import com.ljx.domain.vo.PageVo;
import com.ljx.mapper.RoleMapper;
import com.ljx.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2023-04-21 11:14:18
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        if(id == 1L){
            ArrayList<String> list = new ArrayList<>();
            list.add("admin");
            return list;
        }
        //否则查询用户所具有的角色信息
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult listAll(Integer pageNum, Integer pageSize, String roleName, String status) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.hasText(roleName),Role::getRoleName,roleName);
        lambdaQueryWrapper.eq(StringUtils.hasText(status),Role::getStatus,status);
        Page<Role> page = new Page(pageNum, pageSize);
        page(page,lambdaQueryWrapper);
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);
    }


    @Override
    public ResponseResult changeStatus(RoleStatusDto roleStatusDto) {
        Role role = getById(roleStatusDto.getRoleId());
        role.setStatus(roleStatusDto.getStatus());
        updateById(role);
        return ResponseResult.okResult("更新状态成功");
    }


}
